﻿using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Ymca.DataAccess;
using Ymca.DataAccess.Interfaces;
using Ymca.Entities;
using Ymca.Web.Models.Profile;
using Ymca.Web.Services.Interfaces;

namespace Ymca.Web.Services
{
	public class UserProfileService: IUserProfileService
	{
		private IUnityContainer _container;
		private IDataContext _dataContext;

		public UserProfileService()
		{
			_container = ContainerConfig.Container;
			_dataContext = new DataContext();
		}

		public void AddEmptyProfile(string id, string email)
		{
			UserProfile profile = new UserProfile() { Id = id, Email = email };
			_dataContext.UserProfiles.Add(profile);
			_dataContext.Save();
		}

		public UserProfile GetProfile(string id)
		{
			return _dataContext.UserProfiles.Find(id);
		}

		public void EditProfile(EditProfileViewModel userProfile)
		{
			UserProfile profile = _dataContext.UserProfiles.Find(userProfile.Id);
			profile.Id = userProfile.Id;
			profile.Email = userProfile.Email;
			profile.Info = userProfile.Info;
			profile.Name = userProfile.Name;
			profile.Surname = userProfile.Surname;
			profile.Notify = userProfile.Notify;

			_dataContext.Save();
		}

		public void RemoveProfile(string id)
		{
			UserProfile profile = _dataContext.UserProfiles.Find(id);
			if (profile != null)
			{
				_dataContext.UserProfiles.Remove(profile);
				_dataContext.Save();
				//TODO: REMOVE ALL RELATED DATA
			}
		}
	}
}